﻿namespace MagicWatering.UI
{
    using System;
    using System.Windows.Forms;

    public class FormManager
    {
        #region Static fields and constants

        public static readonly string s_EnabledLabel = "enabled";
        public static readonly string s_DisabledLabel = "disabled";

        private static readonly int BalloonTimeout = 3000;
                                    // preferred timeout (msecs) though .NET enforces 10-sec minimum

        private static readonly int MaxTooltipLength = 63; // framework constraint

        #endregion

        #region Instance fields

        private readonly NotifyIcon s_NotifyIcon;

        #endregion

        #region Constructors and Finalizers

        public FormManager(NotifyIcon notifyIcon)
        {
            s_NotifyIcon = notifyIcon;
        }

        #endregion

        #region Public Methods

        public void BuildContextMenu(ContextMenuStrip contextMenuStrip)
        {
            contextMenuStrip.Items.Clear();
            //contextMenuStrip.Items.AddRange(
            //    new ToolStripItem[]
            //    {
            //        new ToolStripSeparator(),
            //        ToolStripMenuItemWithHandler("Open HOSTS &folder", openHostsFolderItem_Click)
            //    });
        }

        public bool IsEnabled(string cellValue)
        {
            return s_EnabledLabel.Equals(cellValue);
        }

        public ToolStripMenuItem ToolStripMenuItemWithHandler(string displayText, EventHandler eventHandler)
        {
            return ToolStripMenuItemWithHandler(displayText, 0, 0, eventHandler);
        }

        #endregion

        #region Methods

        private ToolStripMenuItem ToolStripMenuItemWithHandler(
            string displayText,
            int enabledCount,
            int disabledCount,
            EventHandler eventHandler)
        {
            var item = new ToolStripMenuItem(displayText);
            if (eventHandler != null)
            {
                item.Click += eventHandler;
            }

            item.Image = (enabledCount > 0 && disabledCount > 0)
                ? Properties.Resources.signal_yellow
                : (enabledCount > 0)
                    ? Properties.Resources.signal_green
                    : (disabledCount > 0) ? Properties.Resources.signal_red : null;
            item.ToolTipText = (enabledCount > 0 && disabledCount > 0)
                ? string.Format("{0} enabled, {1} disabled", enabledCount, disabledCount)
                : (enabledCount > 0)
                    ? string.Format("{0} enabled", enabledCount)
                    : (disabledCount > 0) ? string.Format("{0} disabled", disabledCount) : "";
            return item;
        }

        #endregion
    }
}